package ru.stork.vasilisa.generators

import java.io.FileWriter
import ru.stork.vasilisa.model._

object generateScala extends Generator {
  def apply(model: VasilisaType, destination: String) {
    val w = new StringBuffer()

    model match {
      case model: Model => {

        // write package info
        w.append("package ")
        w.append(printPackage(model.ns.local))
        w.append(";")

        // write imports
        for (i <- model.ns.imports) {
          w.append("import ")
          w.append(printPackage(i.pkg))
          w.append(".")
          w.append(i.name);
          w.append(";")
        }

        w.append("class ");
        w.append(model.name);

      }
      case model: Variant => {

      }
    }
  }

  private def writeProperties(model:Model, w:StringBuffer) {
    for (p <- model.validProperties) {
      w.append("private var _")
      w.append(p.name);
      w.append(":");
      writeType(p.typ, w);
    }
  }

  private def writeType(typ:Type, w:StringBuffer) {
    typ match {
      case Declaration(typ, params, _) => {
        w.append(typ().name)
        params match {
          case Some(x) => {
            w.append("<");
            w.append(x.foldLeft("")((t,x) => t + ", " + writeType(x)))
            w.append(">");
          }
          case None =>
        }
      }
      case Parameter(name, _) => w.append(name);
    }
  }

  private def writeType(typ:Type):String = {
    val w = new StringBuffer()
    writeType(typ, w);
    w.toString
  }
}